﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database.Classes;
using System.Collections.Generic;

public partial class UserDefinedFunctions
{
    
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
    public static SqlInt32 PlanetOwner(int systemId, int planetId)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT OwnerID FROM dbo.Planets Where PlanetID = @pid and SystemId = @sid", conn))
            {
                cmd.Parameters.Add("@sid", SqlDbType.Int).Value = systemId;
                cmd.Parameters.Add("@pid", SqlDbType.Int).Value = planetId;
                SqlDataReader rdr = cmd.ExecuteReader();
                rdr.Read();
                //conn.Close();
                return int.Parse(rdr[0].ToString());
            }
        }
    }
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
    public static SqlInt32 FleetOnPlanetOwner(int systemId, int planetId)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT FleetOwnerID FROM dbo.Planets Where PlanetID = @pid and SystemId = @sid", conn))
            {
                cmd.Parameters.Add("@sid", SqlDbType.Int).Value = systemId;
                cmd.Parameters.Add("@pid", SqlDbType.Int).Value = planetId;
                SqlDataReader rdr = cmd.ExecuteReader();
                rdr.Read();
                //conn.Close();
                return int.Parse(rdr[0].ToString());
            }
        }
    }

    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
    public static SqlBoolean IsEmptyPlanet(int systemId, int planetId)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT OwnerID FROM dbo.Planets Where PlanetID = @pid and SystemId = @sid", conn))
            {
                cmd.Parameters.Add("@sid", SqlDbType.Int).Value = systemId;
                cmd.Parameters.Add("@pid", SqlDbType.Int).Value = planetId;
                SqlDataReader rdr = cmd.ExecuteReader();
                rdr.Read();
                //conn.Close();
                if (rdr.GetInt32(0) == 0)
                    return true;
                else
                    return false;
            }
        }
    }
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
    public static SqlBoolean IsUnknown(int systemId, int planetId)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT OwnerID FROM dbo.Planets Where PlanetID = @pid and SystemId = @sid", conn))
            {
                cmd.Parameters.Add("@sid", SqlDbType.Int).Value = systemId;
                cmd.Parameters.Add("@pid", SqlDbType.Int).Value = planetId;
                SqlDataReader rdr = cmd.ExecuteReader();
                rdr.Read();
                //conn.Close();
                if (rdr.GetInt32(0) == 1)
                    return true;
                else
                    return false;
            }
        }
    }
    public static int PopulationOnPlanet(int sId, int pId)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT PopulationLevel FROM dbo.Planets Where PlanetID = @pid and SystemId = @sid", conn))
            {
                cmd.Parameters.Add("@sid", SqlDbType.Int).Value = sId;
                cmd.Parameters.Add("@pid", SqlDbType.Int).Value = pId;
                return int.Parse(cmd.ExecuteScalar().ToString());
            }
        }
    }
    public static Dictionary<string, int> FleetOnPlanet(int sId, int pId)
    {
        Dictionary<string, int> tmp = new Dictionary<string, int>();
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Planets Where PlanetID = @pid and SystemId = @sid", conn))
            {
                cmd.Parameters.Add("@sid", SqlDbType.Int).Value = sId;
                cmd.Parameters.Add("@pid", SqlDbType.Int).Value = pId;
                SqlDataReader rdr = cmd.ExecuteReader();
                rdr.Read();
                tmp.Add("ColonyShips",(int) double.Parse(rdr["ColonyShips"].ToString()));
                tmp.Add("Transports", (int)double.Parse(rdr["Transports"].ToString()));
                tmp.Add("Destroyers", (int)double.Parse(rdr["Destroyers"].ToString()));
                tmp.Add("Crusiers", (int)double.Parse(rdr["Crusiers"].ToString()));
                tmp.Add("Battleships", (int)double.Parse(rdr["Battleships"].ToString()));
                tmp.Add("Starbase", (int)double.Parse(rdr["Starbase"].ToString()));
            }
        }
        return tmp;
    }
};

